﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;
using SettlersEngine;
using SettlersEngine.Exported;

namespace CommunitySettlers
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MainWindow Instance { get; private set; }

        public GameInstance Game { get; private set; }
        public BindingList<BuildableListEntry> BuildableList { get; private set; }

        public class BuildableListEntry
        {
            public String Tooltip { get; set; }
            public ImageSource Source { get; set; }
            public Int32 TypeIndex { get; set; }
        }

        public MainWindow()
        {
            Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;

            InitializeComponent();
            
            
        }

        void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;

            MessageBox.Show(e.Exception.ToString(), "Unknown error...", MessageBoxButton.OK, MessageBoxImage.Error);

            StreamWriter writer = File.AppendText(".\\log.txt");

            using (writer)
            {
                writer.WriteLine(e.Exception.ToString());
            }
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            BuildableListEntry entry = ((Border)sender).Tag as BuildableListEntry;

            if (entry != null)
                Game.ShowBuildingGrid(entry.TypeIndex);
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }


        private void BTN_ToggleBuildingConfig_Checked(object sender, RoutedEventArgs e)
        {
            if (BTN_ToggleBuildingConfig.IsChecked.Value)
            {
                // determine which config tab to show
                WIZARD_MainWizard.SelectedItem = TAB_WarehouseConfig;
            }
            else
                WIZARD_MainWizard.SelectedItem = TAB_MainTabControl;
        }

        private void BTN_WarehouseAdd_Click(object sender, RoutedEventArgs e)
        {
            Resource res = (Resource)((Button)sender).Tag;
            BuildingListener building = Game.Selection.Building;

        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            Game = GameInstance.CreateSinglePlayer(new GameSetup(Guid.Empty));
            Game.Surface = PANEL_GameSurface;
            Game.Open();

            BuildableList = new BindingList<BuildableListEntry>();
            LIST_Buildable.ItemsSource = BuildableList;
            DataContext = this;

            foreach (var buildable in Game.Races[0].Buildables)
            {
                BuildableList.Add(new BuildableListEntry()
                {
                    Source = Game.GetThumbnailForBuilding(buildable.TypeIndex),
                    Tooltip = buildable.Name,
                    TypeIndex = buildable.TypeIndex,
                });
            }

            Game.Selection.OnChanged += (unused) =>
            {
                PANEL_SelectionDetails.DataContext = new Object();
                PANEL_SelectionDetails.DataContext = Game.Selection;
                TAB_WarehouseConfig.DataContext = new Object();
                TAB_WarehouseConfig.DataContext = this;
            };

            Instance = this;

        }

        private void BTN_Dump_Click(object sender, RoutedEventArgs e)
        {
            Game.Dump();
        }
        
    }
}
